package com.alphathink.deliveryassist.message.templates;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.AlphaThink.DeliveryAssist.R;

/**
 * 模板管理
 * 
 * @author click
 * 
 */
public class ModelActivity extends Activity {
	private Button button01 = null;
	private EditText editText01 = null, editTextName = null;
	private Spinner spinner = null;
	private List<String> list = new ArrayList<String>();
	private ArrayAdapter<String> adapter;
	private ImageButton mMesModelBackIB;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);

		setContentView(R.layout.model);

		// mMesModelBackIB = (ImageButton)
		// findViewById(R.id.IB_back_TemptitleBar);
		// mMesModelBackIB.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // Intent intent = new Intent(ModelActivity.this,
		// // MainActivity.class);
		// // startActivity(intent);
		// // ModelActivity.this.finish();
		// Runtime runtime = Runtime.getRuntime();
		// try {
		// runtime.exec("input keyevent " + KeyEvent.KEYCODE_BACK);
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		// });
		editText01 = (EditText) findViewById(R.id.Model_EditText01);
		editTextName = (EditText) findViewById(R.id.Model_EditTextName);

		spinner = (Spinner) findViewById(R.id.Model_Spinner01);
		list.add("模块一");
		list.add("模块二");
		list.add("模块三");
		list.add("模块四");
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				SharedPreferences share = getSharedPreferences("model",
						MODE_PRIVATE);
				if (position == 0) {
					editTextName.setText(share.getString("modelName01", "模块一"));
					editText01.setText(share.getString("modelMessage01", ""));
				} else if (position == 1) {
					editTextName.setText(share.getString("modelName02", "模块二"));
					editText01.setText(share.getString("modelMessage02", ""));
				} else if (position == 2) {
					editTextName.setText(share.getString("modelName03", "模块三"));
					editText01.setText(share.getString("modelMessage03", ""));
				} else if (position == 3) {
					editTextName.setText(share.getString("modelName04", "模块四"));
					editText01.setText(share.getString("modelMessage04", ""));
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}

		});

		button01 = (Button) findViewById(R.id.Model_Button01);
		button01.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SharedPreferences share = getSharedPreferences("model",
						MODE_PRIVATE);
				SharedPreferences.Editor edit = share.edit(); // 编辑文件
				if (spinner.getSelectedItem().toString().equals("模块一")) {
					edit.putString("modelName01", editTextName.getText()
							.toString());
					edit.putString("modelMessage01", editText01.getText()
							.toString());
				} else if (spinner.getSelectedItem().toString().equals("模块二")) {
					edit.putString("modelName02", editTextName.getText()
							.toString());
					edit.putString("modelMessage02", editText01.getText()
							.toString());
				} else if (spinner.getSelectedItem().toString().equals("模块三")) {
					edit.putString("modelName03", editTextName.getText()
							.toString());
					edit.putString("modelMessage03", editText01.getText()
							.toString());
				} else if (spinner.getSelectedItem().toString().equals("模块四")) {
					edit.putString("modelName04", editTextName.getText()
							.toString());
					edit.putString("modelMessage04", editText01.getText()
							.toString());
				}
				edit.commit(); // 保存数据信息
				Toast.makeText(getApplicationContext(), "保存成功",
						Toast.LENGTH_SHORT).show();
			}
		});

	}
}
